package com.kly.cashmall.module.products;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarFragment;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.IndexProductListBean;
import com.kly.cashmall.bean.ProductItemBaen;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.products.adapter.ProductsListAdapter;
import com.kly.cashmall.module.products.presenter.ProductsPresenter;
import com.kly.cashmall.module.products.presenter.ProductsViewer;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
public class ProductsFragment extends BaseBarFragment implements ProductsViewer {

    private static final int LIST_SIZE = 10;
    private RecyclerView recyclerView;
    private List<ProductItemBaen> productDataList = new ArrayList<>();
    private SmartRefreshLayout mSmartRefreshLayout;

    private int currentPage = 1;
    private int amountValue = 1;//1 升序 2降序
    private int popularityValue = 2;//1 升序 2降序
    @PresenterLifeCycle
    ProductsPresenter productsPresenter = new ProductsPresenter(this);
    private JsonArray filterConditions;
    private TextView mProductsFilterAmountFont;
    private TextView mProductsFilterPopularityFont;
    private ImageView mProductsFilterAmountImg;
    private ImageView mProductsFilterPopularityImg;
    private ProductsListAdapter mAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_loan_products_list;
    }

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setActionBar(false);
        filterConditions = new JsonArray();
        mSmartRefreshLayout = bindView(R.id.smartRefreshLayout);
        mProductsFilterAmountFont = bindView(R.id.products_filter_amount_font);
        mProductsFilterAmountImg = bindView(R.id.products_filter_amount_img);
        mProductsFilterPopularityFont = bindView(R.id.products_filter_popularity_font);
        mProductsFilterPopularityImg = bindView(R.id.products_filter_popularity_img);
        recyclerView = bindView(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
        mAdapter = new ProductsListAdapter();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            BaseActionHelper.with(getContext()).handleAction(mAdapter.getItem(position).getButtonJumpUrl(), true);
        });
        recyclerView.setAdapter(mAdapter);
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                currentPage++;
                productsPresenter.getProductList(currentPage, filterConditions);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                currentPage = 1;
                productsPresenter.getProductList(currentPage, filterConditions);
            }
        });
        bindView(R.id.products_filter_amount, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickAmout();
            }
        });
        bindView(R.id.products_filter_popularity, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickPopularity();
            }
        });
    }

    /**
     * 根据受欢迎程度进行筛选
     */
    private void clickPopularity() {
        currentPage = 1;
        filterConditions = new JsonArray();
        popularityValue = (popularityValue == 1 ? 2 : 1);
        JsonObject object = new JsonObject();
        object.addProperty("name", "popularity");
        object.addProperty("value", popularityValue);
        filterConditions.add(object);
        productsPresenter.getProductList(currentPage, filterConditions);
        if (popularityValue == 1) {
            mProductsFilterPopularityImg.setImageResource(R.mipmap.ic_product_up);
        } else {
            mProductsFilterPopularityImg.setImageResource(R.mipmap.ic_product_down);
        }
        mProductsFilterAmountImg.setImageResource(R.mipmap.ic_product_down);

        mProductsFilterAmountFont.setTextColor(getActivity().getResources().getColor(R.color.color_b3ffffff));
        mProductsFilterPopularityFont.setTextColor(Color.WHITE);
    }

    /**
     * 根据金额筛选
     */
    private void clickAmout() {
        currentPage = 1;
        filterConditions = new JsonArray();
        amountValue = (amountValue == 1 ? 2 : 1);
        JsonObject object = new JsonObject();
        object.addProperty("name", "amount");
        object.addProperty("value", amountValue);
        filterConditions.add(object);
        productsPresenter.getProductList(currentPage, filterConditions);
        if (amountValue == 1) {
            mProductsFilterAmountImg.setImageResource(R.mipmap.ic_product_up);
        } else {
            mProductsFilterAmountImg.setImageResource(R.mipmap.ic_product_down);
        }
        mProductsFilterPopularityImg.setImageResource(R.mipmap.ic_product_down);

        mProductsFilterAmountFont.setTextColor(Color.WHITE);
        mProductsFilterPopularityFont.setTextColor(getActivity().getResources().getColor(R.color.color_b3ffffff));
    }

    @Override
    protected void loadData() {

    }

    /**
     * 下拉刷新
     *
     * @param productList
     */
    @Override
    public void onProductListRefreshingSuccess(IndexProductListBean productList) {
        if (productList.getCategorys().size() < LIST_SIZE) {
            mSmartRefreshLayout.setEnableLoadMore(false);
        } else {
            mSmartRefreshLayout.setEnableLoadMore(true);
        }
        if (mSmartRefreshLayout.getState() == RefreshState.Refreshing) {
            mSmartRefreshLayout.finishRefresh();
        }
        productDataList.clear();
        productDataList.addAll(productList.getCategorys());
        mAdapter.setList(productDataList);
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 加载更多
     *
     * @param productList
     */
    @Override
    public void onProductListLoadmoreSuccess(IndexProductListBean productList) {
        if (productList.getCategorys().size() < LIST_SIZE) {
            mSmartRefreshLayout.setEnableLoadMore(false);
        } else {
            mSmartRefreshLayout.setEnableLoadMore(true);
        }
        if (mSmartRefreshLayout.getState() == RefreshState.Loading) {
            mSmartRefreshLayout.finishLoadMore();
        }
        productDataList.addAll(productList.getCategorys());
        mAdapter.setList(productDataList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onProductListRefreshingFailed() {

    }

    @Override
    public void onProductListLoadmoreFailed() {
        currentPage--;
    }

    @Override
    public void onResume() {
        super.onResume();
        productsPresenter.getProductList(currentPage, filterConditions);
    }
}
